package com.wang.conf;

import com.wang.packpojo.ConHeart;
import com.wang.packpojo.ConMsg;
import com.wang.pojo.*;
import com.wang.service.*;
import com.wang.socket.SocketMap;
import com.wang.util.ModBusUtil;
import com.wang.util.SpringUtil;
import io.netty.channel.ChannelHandlerContext;

import java.net.InetSocketAddress;
import java.util.*;

public class DevManager {


    //更新设备数据
    public static void updateDevDate(String devGId) {


        //判断设备是否在线
        Boolean flag = false;
        ConMsg conMsg = null;

        for (Map.Entry<String, ConMsg> entry : SocketMap.mRegPassDeviceMap.entrySet()) {

            if (entry.getValue().getDevice().getDevGId().equals(devGId)) {
                flag = true;
                conMsg = entry.getValue();

            }
        }
        for (Map.Entry<String, ConMsg> entry : SocketMap.jRegPassDeviceMap.entrySet()) {

            if (entry.getValue().getDevice().getDevGId().equals(devGId)) {
                flag = true;
                conMsg = entry.getValue();
            }
        }
        for (Map.Entry<String, ConMsg> entry : SocketMap.c2RegPassDeviceMap.entrySet()) {

            if (entry.getValue().getDevice().getDevGId().equals(devGId)) {
                flag = true;
                conMsg = entry.getValue();
            }
        }


        //更新注册的舍设备数据
        if (flag) {

            //更新数据
            DeviceService deviceService = SpringUtil.getBean("deviceServiceImpl", DeviceServiceImpl.class);
            Device device = deviceService.selectDeviceByDevGId(conMsg.getDevice().getDevGId());

            //查询传输配置
            DevConConfigService devConConfigService = SpringUtil.getBean("devConConfigServiceImpl", DevConConfigServiceImpl.class);

            DevConConfig devConConfig = devConConfigService.selectConfigByDevId(device.getDevId());
            device.setTxDataType(devConConfig.getDevDataTypeCode());
            device.setTxType(devConConfig.getDevTxTypeCode());


            ConMsg newConMsg = new ConMsg();
            newConMsg.setDevice(device);
            newConMsg.setCtx(conMsg.getCtx());
            //添加心跳包
            ConHeart conHeart = new ConHeart();
            newConMsg.setConHeart(conHeart);
            newConMsg.setConnectTime(new Date().getTime());
            //设置数据传输方式
            newConMsg.setTxDataType(device.getTxDataType());
            //1.先单独添加采集频率

            //采集频率
            DevCollectRateService devCollectRateService = SpringUtil.getBean("devCollectRateServiceImpl", DevCollectRateServiceImpl.class);
            DevCollectRate devCollectRate = devCollectRateService.selectCollectRateByDevId(device.getDevId());

            newConMsg.setCollectRate(Integer.parseInt(devCollectRate.getDevCrValue()));

            //心跳包
            DevConConfigService configService = SpringUtil.getBean("devConConfigServiceImpl", DevConConfigServiceImpl.class);
            DevConConfig conConfig = configService.selectConfigByDevId(device.getDevId());

            newConMsg.getConHeart().setHeartBeat(conConfig.getDevHeart());

            //数据开关
            DevSwitchService devSwitchService = SpringUtil.getBean("devSwitchServiceImpl", DevSwitchServiceImpl.class);
            DevSwitch devSwitch = devSwitchService.selectByDevId(device.getDevId());
            newConMsg.setIsOpen(devSwitch.getDevSwitchValue());


            //如果是modbus 设备

            //添加modbus数据解析配置
            //查询modbus 配置


            if (device.getTxDataType().equals(AppData.ModbusDataCode)) {

                DevMdtParamConfService mdtParamConfService = SpringUtil.getBean("devMdtParamConfServiceImpl", DevMdtParamConfServiceImpl.class);

                List<DevMdtParamConf> mdtParamConfList = mdtParamConfService.selectDevMdtParamConfByDevId(device.getDevId());

                //指令集合
                Map<String, DevMdtParamConf> commandMap = new HashMap<>();
                DevMdtParamConfDataService mdtParamConfDataService = SpringUtil.getBean("devMdtParamConfDataServiceImpl", DevMdtParamConfDataServiceImpl.class);
                for (DevMdtParamConf mdtParamConf : mdtParamConfList) {

                    List<DevMdtParamConfData> mdtParamConfDataList = mdtParamConfDataService.selectDevMdtParamConfDataByConfId(mdtParamConf.getConfId());
                    mdtParamConf.setParamConfDataList(mdtParamConfDataList);

                    String command = ModBusUtil.makeUpCommand(mdtParamConf, mdtParamConfDataList);

                    commandMap.put(command, mdtParamConf);

                }

                newConMsg.setCommandMap(commandMap);
                newConMsg.setTxConf(mdtParamConfList);


                //原地址保留
                String address = getRemoteAddress(conMsg.getCtx());


                SocketMap.mRegPassDeviceMap.put(address, newConMsg);

            } else if(device.getTxDataType().equals(AppData.JSONDataCode)){
                //json配置
                //添加json配置
                //查询json配置 目前json 数据配置只配置一个 后面有需求在扩展

                DevJdtParamConfService jdtParamConfService = SpringUtil.getBean("devJdtParamConfServiceImpl", DevJdtParamConfServiceImpl.class);
                DevJdtParamConf jdtParamConf = jdtParamConfService.selectDevJdtParamConfByDevId(device.getDevId());

                //查询具体参数配置
                DevJdtParamConfDataService jdtParamConfDataService = SpringUtil.getBean("devJdtParamConfDataServiceImpl", DevJdtParamConfDataServiceImpl.class);

                List<DevJdtParamConfData> jdtParamConfDataList = jdtParamConfDataService.selectDevJdtParamConfDataByConfId(jdtParamConf.getConfId());

                jdtParamConf.setParamConfDataList(jdtParamConfDataList);
                //设置传输配置
                conMsg.setTxConf(jdtParamConf);

                //添加json注册成功 全局变量

                //原地址保留
                String address = getRemoteAddress(conMsg.getCtx());
                SocketMap.jRegPassDeviceMap.put(address, newConMsg);

            }else {

                //原地址保留
                String address = getRemoteAddress(conMsg.getCtx());


                SocketMap.c2RegPassDeviceMap.put(address, newConMsg);
            }

        }
    }

    //获取连接ctx的地址
    public static String getRemoteAddress(ChannelHandlerContext ctx) {
        InetSocketAddress ipSocket = (InetSocketAddress) ctx.channel().remoteAddress();
        String clientIp = ipSocket.getAddress().getHostAddress();
        Integer port = ipSocket.getPort();
        String ip = clientIp + ":" + port;
        return ip;
    }

}
